package utils.ai
{
	import global.GameType;
	
	import model.card.ICardData;
	
	import utils.rule.IRuleResult;
	import utils.rule.RuleResult;

	public class CheckPlayCard
	{
		//检查出一张牌
		public static function play1Card(cardDataS:Array):IRuleResult
		{
			var ruleResult:IRuleResult = new RuleResult();
			if (cardDataS.length != 1)
			{
				return ruleResult;
			}
			
			var cardData:ICardData = cardDataS[0];
			if (cardData.value != GameType.CARD_VALUE_JOKER)
			{
				ruleResult.correct = true;
				ruleResult.results = [CardTools.cloneCardDatas(cardDataS)];
			}
			return ruleResult;
		}
		
		//检查两张牌
		public static function play2Card(cardDataS:Array):IRuleResult
		{
			var ruleResult:IRuleResult = new RuleResult();
			if (cardDataS.length != 2)
			{
				return ruleResult;
			}
			
			//cardDataS = CardTools.cloneCardDatas(cardDataS);
			var jokers:Array = CardTools.getJokers(cardDataS);
			var rest:Array = CardTools.getRest(cardDataS);

			if (CardTools.isCardDatasTheSame(cardDataS))//是否都一样
			{
				ruleResult.results = [CardTools.cloneCardDatas(cardDataS)];
				ruleResult.correct = true;
			}
			else if (jokers.length == 1)//只有一个王，匹配着出
			{
				if (rest.length <= 3)
				{
					ruleResult.results = [ CardTools.setJokerAsOthers(jokers[0],rest)];
					ruleResult.correct = true;
				}
			}
			return ruleResult;
		}
		
		//检查3张牌
		public static function playMoreThan3Card(cardDataS:Array):IRuleResult
		{
			var ruleResult:IRuleResult = new RuleResult();
			if (cardDataS.length < 3)
			{
				return ruleResult;
			}
			
			var jokers:Array = CardTools.getJokers(cardDataS);
			if (jokers.length == 2)//对王不能一起变着出
			{
				return ruleResult;	
			}
			
			if (jokers.length == 0)
			{
				if (CardTools.isCardDatasStraight(cardDataS))
				{
					ruleResult.correct = true;
					ruleResult.isStraight = true;
					ruleResult.results = [CardTools.cloneCardDatas(cardDataS)];
				}
				else if (CardTools.isCardDatasTheSame(cardDataS))
				{
					ruleResult.correct = true;
					ruleResult.results = [CardTools.cloneCardDatas(cardDataS)];
				}
				else if (CardTools.isPairStraight(cardDataS).correct)//如果是连对
				{
					ruleResult.correct = true;
					ruleResult.results = [CardTools.cloneCardDatas(cardDataS)];
				}
			}
			else if (jokers.length == 1)//一张王
			{
				var rest:Array = CardTools.getRest(cardDataS);
				if (CardTools.isCardDatasTheSame(rest))
				{
					if (rest.length <= 3)
					{
						ruleResult.correct = true;
						ruleResult.results = [CardTools.setJokerAsOthers(jokers[0],rest)];
					}
				}
				else//连牌配王
				{
					var combineCardDataS:Array = CardTools.combineJokerWithRestToStraight(jokers[0],rest);
					if (combineCardDataS.length == 1 || combineCardDataS.length == 2)
					{
						ruleResult.correct = true;
						ruleResult.results = combineCardDataS;
					}
					else
					{
						var pairStarght:Array = CardTools.setJokerPairStarightGap(jokers[0],rest);
						if (pairStarght.length == 1)
						{
							ruleResult.correct = true;
							ruleResult.results = pairStarght;
						}
					}
				}
			}
			
			return ruleResult;
		}
	}
}